package com.ty.dao.impl;

import com.ty.bean.Menu;
import com.ty.bean.Role;
import com.ty.bean.Users;
import com.ty.dao.DBUtils;
import com.ty.dao.RoleDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 田野
 */
public class RoleDaoImpl extends DBUtils implements RoleDao {
    @Override
    public List<Role> getRoleList(int pageIndex, int pageSize) {
        List params = new ArrayList();
        params.add((pageIndex-1)*pageSize);
        params.add(pageSize);

        List<Role> roleList = new ArrayList<>();
        try {
            // 双表联查
            String sql = " select * from role limit ?, ? ";
            resultSet = query(sql, params);

            while (resultSet.next()){
                Role role = new Role();

                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));

                roleList.add(role);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }


        return roleList;
    }

    @Override
    public int total() {
        int total = 0;

        try {
            String sql = " select count(*) from role ";
            resultSet = query(sql, null);

            while (resultSet.next()){
                total = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return total;
    }

    @Override
    public int insertRole(Role role) {
        // 新增数据id
        int key = 0;
        List params = new ArrayList();

        try {
            String sql = " insert into role values(null, ?, ?)";
            params.add(role.getRoleName());
            params.add(role.getRoleState());

            // update保存的是受影响的行数
            int update = update(sql, params);

            ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
            if (generatedKeys.next()){
                key = generatedKeys.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return key;
    }

    @Override
    public Role findById(int roleId) {
        // 一个角色对应多个菜单
        Role role = new Role();
        List<Menu> menuList = new ArrayList<>();

        List params = new ArrayList();
        params.add(roleId);

        try {
            String sql = " select * from role r, menu m, middle mid where r.roleid=mid.roleid and " +
                    " mid.menuid=m.menuid and r.roleid=? ";

            resultSet = query(sql, params);

            while (resultSet.next()){
                // 封装Role对象
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));

                // 封装Menu对象
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpMenuId(resultSet.getInt("upmenuid"));
                menu.setState(resultSet.getInt("state"));
                menu.setDesc(resultSet.getString("desc"));
                menu.setUrl(resultSet.getString("url"));

                menuList.add(menu);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        // 建立关系，一个角色对应多个菜单
        role.setMenuList(menuList);

        return role;
    }
}
